package cn.com.visionsky.bocsz.bda.webservice.permission;

import cn.com.visionsky.bocsz.bda.webservice.Constants;
import cn.com.visionsky.bocsz.bda.webservice.cache.FieldPermissionsCache;
import cn.com.visionsky.bocsz.bda.webservice.handler.customer_tags_query.FullTextRequest;
import cn.com.visionsky.bocsz.bda.webservice.ws.Reply;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Map;

/**
 * Created by zhouqiufu on 2018/1/30.
 */
public class CustomerTagsPermission {
    private static final Logger logger = LoggerFactory.getLogger(CustomerTagsPermission.class);

    private String clientIp;
    private FullTextRequest fullTextRequest;

    public CustomerTagsPermission(FullTextRequest fullTextRequest, String clientIp){
        this.fullTextRequest = fullTextRequest;
        this.clientIp = clientIp;
    }

    public Reply verifyCustomerTagsPermission(){

        String etlDate = fullTextRequest.getEtlDate();
        String ctfNo = fullTextRequest.getCtfNo();
        String cstNm = fullTextRequest.getCstNm();
        String sex = fullTextRequest.getSex();
        String birthday = fullTextRequest.getBirthday();
        String age = fullTextRequest.getAge();
        String acNoGjj = fullTextRequest.getAcNoGjj();
        String preSalary = fullTextRequest.getPreSalary();
        String isBocStaff = fullTextRequest.getIsBocStaff();
        String lvlKyc = fullTextRequest.getLvlKyc() ;
        String mobphnNo = fullTextRequest.getMobphnNo();
        String telCheck = fullTextRequest.getTelCheck();
        String occuCode = fullTextRequest.getOccuCode();
        String isGat = fullTextRequest.getIsGat();
        String nationality = fullTextRequest.getNationality();
        String nation = fullTextRequest.getNation();
        String homeAdr = fullTextRequest.getHomeAdr();
        String unitAdr = fullTextRequest.getUnitAdr();
        String email = fullTextRequest.getEmail();
        String maritalStatus = fullTextRequest.getMaritalStatus();
        String educationCode = fullTextRequest.getEducationCode();
        String unitNm = fullTextRequest.getUnitNm();
        String isMgr = fullTextRequest.getIsMgr();
        String groupCode = fullTextRequest.getGroupCode();
        String cstId = fullTextRequest.getCstId();
        //String mode = fullTextRequest.getMode();//选择模式（1.根据客户号查询 2.根据客户证件号查询）该字段不必进行权限控制

        //输入参数权限控制，根据参数是否有值，进而判断是否需要将该参数列入比较的队列
        //如果输入参数涉及未授权字段，则立即返回权限过低的返回码。

        final Reply failureReply = new Reply();
        failureReply.setResultCode(Constants.PERMISSION_DENIED_CODE);//拒绝访问
        failureReply.setResultMsg(Constants.PERMISSION_DENIED_MSG);

        Map<String, List<String>> fieldPermissionsCacheMap = FieldPermissionsCache.getFieldPermissionsCacheMap();
        if(fieldPermissionsCacheMap == null || fieldPermissionsCacheMap.isEmpty()){
            return failureReply;
        }

        String key = clientIp + Constants.CUSTOMER_TAGS_QUERY_HANDLER_MODULE.toUpperCase();//ip+接口名称
        List<String> fieldPermissionsList = fieldPermissionsCacheMap.get(key);
        logger.info("fieldPermissionsList:           "+fieldPermissionsList);
        if(fieldPermissionsList == null || fieldPermissionsList.isEmpty()){
            return failureReply;
        }

        Reply successReply = new Reply();
        successReply.setResultCode(Constants.PERMISSIONS_ENOUGH_CODE);//权限足够
        successReply.setResultMsg(Constants.PERMISSIONS_ENOUGH_MSG);

        final int ONE_RESULT = 1;
        final String ALL_FIELD = "*";
        if(fieldPermissionsList.size() == ONE_RESULT){
            if(fieldPermissionsList.contains(ALL_FIELD)){
                return successReply;
            }
        }

        if(StringUtils.isNotBlank(etlDate) && (!fieldPermissionsList.contains(Constants.CUSTOMER_TAGS_QUERY_MOUDLE_ETL_DATE))){
            return failureReply;
        }
        /*
        if(StringUtils.isNotBlank(ctfNo) && (!fieldPermissionsList.contains(Constants.CUSTOMER_TAGS_QUERY_MOUDLE_CTF_NO))){
            return failureReply;
        }
        */
        if(StringUtils.isNotBlank(cstNm) && (!fieldPermissionsList.contains(Constants.CUSTOMER_TAGS_QUERY_MOUDLE_CST_NM))){
            return failureReply;
        }
        if(StringUtils.isNotBlank(sex) && (!fieldPermissionsList.contains(Constants.CUSTOMER_TAGS_QUERY_MOUDLE_SEX))){
            return failureReply;
        }
        if(StringUtils.isNotBlank(birthday) && (!fieldPermissionsList.contains(Constants.CUSTOMER_TAGS_QUERY_MOUDLE_BIRTHDAY))){
            return failureReply;
        }
        if(StringUtils.isNotBlank(age) && (!fieldPermissionsList.contains(Constants.CUSTOMER_TAGS_QUERY_MOUDLE_AGE))){
            return failureReply;
        }
        if(StringUtils.isNotBlank(acNoGjj) && (!fieldPermissionsList.contains(Constants.CUSTOMER_TAGS_QUERY_MOUDLE_AC_NO_GJJ))){
            return failureReply;
        }
        if(StringUtils.isNotBlank(preSalary) && (!fieldPermissionsList.contains(Constants.CUSTOMER_TAGS_QUERY_MOUDLE_PRE_SALARY))){
            return failureReply;
        }
        if(StringUtils.isNotBlank(isBocStaff) && (!fieldPermissionsList.contains(Constants.CUSTOMER_TAGS_QUERY_MOUDLE_IS_BOC_STAFF))){
            return failureReply;
        }
        if(StringUtils.isNotBlank(lvlKyc) && (!fieldPermissionsList.contains(Constants.CUSTOMER_TAGS_QUERY_MOUDLE_LVL_KYC))){
            return failureReply;
        }
        if(StringUtils.isNotBlank(mobphnNo) && (!fieldPermissionsList.contains(Constants.CUSTOMER_TAGS_QUERY_MOUDLE_MOBPHN_NO))){
            return failureReply;
        }
        if(StringUtils.isNotBlank(telCheck) && (!fieldPermissionsList.contains(Constants.CUSTOMER_TAGS_QUERY_MOUDLE_TEL_CHECK))){
            return failureReply;
        }
        if(StringUtils.isNotBlank(occuCode) && (!fieldPermissionsList.contains(Constants.CUSTOMER_TAGS_QUERY_MOUDLE_OCCU_CODE))){
            return failureReply;
        }
        if(StringUtils.isNotBlank(isGat) && (!fieldPermissionsList.contains(Constants.CUSTOMER_TAGS_QUERY_MOUDLE_IS_GAT))){
            return failureReply;
        }
        if(StringUtils.isNotBlank(nationality) && (!fieldPermissionsList.contains(Constants.CUSTOMER_TAGS_QUERY_MOUDLE_NATIONALITY))){
            return failureReply;
        }
        if(StringUtils.isNotBlank(nation) && (!fieldPermissionsList.contains(Constants.CUSTOMER_TAGS_QUERY_MOUDLE_NATION))){
            return failureReply;
        }
        if(StringUtils.isNotBlank(homeAdr) && (!fieldPermissionsList.contains(Constants.CUSTOMER_TAGS_QUERY_MOUDLE_HOME_ADR))){
            return failureReply;
        }
        if(StringUtils.isNotBlank(unitAdr) && (!fieldPermissionsList.contains(Constants.CUSTOMER_TAGS_QUERY_MOUDLE_UNIT_ADR))){
            return failureReply;
        }
        if(StringUtils.isNotBlank(email) && (!fieldPermissionsList.contains(Constants.CUSTOMER_TAGS_QUERY_MOUDLE_EMAIL))){
            return failureReply;
        }
        if(StringUtils.isNotBlank(maritalStatus) && (!fieldPermissionsList.contains(Constants.CUSTOMER_TAGS_QUERY_MOUDLE_MARITAL_STATUS))){
            return failureReply;
        }
        if(StringUtils.isNotBlank(educationCode) && (!fieldPermissionsList.contains(Constants.CUSTOMER_TAGS_QUERY_MOUDLE_EDUCATION_CODE))){
            return failureReply;
        }
        if(StringUtils.isNotBlank(unitNm) && (!fieldPermissionsList.contains(Constants.CUSTOMER_TAGS_QUERY_MOUDLE_UNIT_NM))){
            return failureReply;
        }
        if(StringUtils.isNotBlank(isMgr) && (!fieldPermissionsList.contains(Constants.CUSTOMER_TAGS_QUERY_MOUDLE_IS_MGR))){
            return failureReply;
        }
        if(StringUtils.isNotBlank(groupCode) && (!fieldPermissionsList.contains(Constants.CUSTOMER_TAGS_QUERY_MOUDLE_GROUP_CODE))){
            return failureReply;
        }
        /*
        if(StringUtils.isNotBlank(cstId) && (!fieldPermissionsList.contains(Constants.CUSTOMER_TAGS_QUERY_MOUDLE_CST_ID))){
            return failureReply;
        }
        */

        return successReply;
    }

}
